//
//  TutorMainControl.m
//  blackjack
//
//  Created by kyocooro on 4/2/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "TutorMainControl.h"
#import "EpImageProcessor.h"
#import <QuartzCore/QuartzCore.h>


@interface TutorMainControl(Private)

const int USER_CARD_POS_X = 80;
const int USER_CARD_POS_Y = 150;

int dealerCards_[5];
int playerCards_[5];

UIImageView *upCard_;
UIImageView *downCard_;
UIImageView *playerCardsView_[5];

- (void)setLandscapeToView:(UIView *)view;
- (UIImage *)getCard:(int)no andType:(int)type;
- (void)dealCardTo:(CGPoint)pos withView:(UIView *)view;
- (void)dealToDealer;
- (void)dealToPlayer;



@end


@implementation TutorMainControl

@synthesize delegate;

- (IBAction)hitBtnTouched {
	
}
- (IBAction)standBtnTouched {
	
}
- (IBAction)backMainMenuBtnTouched {
	[delegate backToMainMenu];
}
- (IBAction)dealBtnTouched {
	[self dealToDealer];
	[self dealToPlayer];
	[hitBtn_ setEnabled:YES];
	[standBtn_ setEnabled:YES];
	[dealBtn_ setEnabled:NO];
	
}
- (IBAction)splitBtnTouched {

}
- (IBAction)doubleBtnTouched {

}
- (IBAction)musicBtnTouched {

}
- (IBAction)chooseCountBtnTouched {

}
- (IBAction)chooseModeBtnTouched {

}

- (UIImage *)getCard:(int)no andType:(int)type
{
	UIImage *deck = [UIImage imageNamed:@"classic-playing-cards.png"];
	CGRect cardRect = CGRectMake((no - 1) * 73, type * 98, 72, 98);
	return [EpImageProcessor createSubImage:deck withRect:cardRect];
}
- (void)dealToDealer
{
	dealerCards_[0] = arc4random() % 13 + 1;
	upCard_ = [[UIImageView alloc] initWithFrame:animator_.frame];
	[upCard_ setImage:[self getCard:dealerCards_[0] andType:arc4random() % 4]];
	[self.view addSubview:upCard_];
	[self dealCardTo:CGPointMake(20, 20) withView:upCard_];
	[upCard_ release];
	
	dealerCards_[1] = arc4random() % 13 + 1;
	downCard_ = [[UIImageView alloc] initWithFrame:animator_.frame];
	[downCard_ setImage:[UIImage imageNamed:@"PlayCard1AL.png"]];
	[self.view addSubview:downCard_];
	[self dealCardTo:CGPointMake(100, 20) withView:downCard_];
	[downCard_ release];
}
- (void)dealToPlayer
{
	for (int i = 0; i < 2; i++)
	{
		playerCards_[i] = arc4random() % 13 + 1;
		playerCardsView_[i] = [[UIImageView alloc] initWithFrame:animator_.frame];
		[playerCardsView_[i] setImage:[self getCard:playerCards_[i] andType:arc4random() % 4]];
		[self.view addSubview:playerCardsView_[i]];
		[self dealCardTo:CGPointMake(USER_CARD_POS_X * i + 20, USER_CARD_POS_Y) withView:playerCardsView_[i]];
		[playerCardsView_[i] release];
	}
}
- (void)dealCardTo:(CGPoint)pos withView:(UIView *)view
{
	
	//CABasicAnimation *translationX;	
	//	translationX=[CABasicAnimation animationWithKeyPath:@"transform.translation.x"];
	//	translationX.duration=.3;
	//	translationX.repeatCount=1;
	//	translationX.fillMode = kCAFillModeBoth;
	//	translationX.fromValue=[NSNumber numberWithFloat:animator_.frame.origin.x];
	//	translationX.toValue=[NSNumber numberWithFloat:200];
	//	
	//	CABasicAnimation *translationY;
	//	translationY=[CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
	//	translationY.duration=.3;
	//	translationY.repeatCount=1;
	//	translationY.fillMode = kCAFillModeBoth;
	//	translationY.fromValue=[NSNumber numberWithFloat:animator_.frame.origin.y];
	//	translationY.toValue=[NSNumber numberWithFloat:100];
	//	
	//	CAAnimationGroup *theGroup = [CAAnimationGroup animation];
	//	theGroup.delegate = self;
	//	theGroup.duration = .3;
	//	theGroup.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
	//	theGroup.animations = [NSArray arrayWithObjects:translationX, translationY, nil];
	//	[animator_.layer addAnimation:theGroup forKey:@"dealCard"];
	CGRect frame = view.frame;
	frame.origin = pos;// final y destination
	
	[UIView beginAnimations:@"movingCard" context:nil];
	[UIView setAnimationDuration:.5];
	[view setFrame:frame];
	//[UIView setAnimationDelay:1];
	//translation_.transform = CGAffineTransformMakeRotation(M_PI_2);
	[UIView commitAnimations];
	
}
- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag
{
	//	[player1_ setImage:[self getCard:7 andType:HEART]];
}

- (void)viewDidLoad {
    [super viewDidLoad];
	[self setLandscapeToView:self.view];
	[hitBtn_ setHidden:YES];
	[doubleBtn_ setHidden:YES];
	[splitBtn_ setHidden:YES];
	[standBtn_ setHidden:YES];
	
	[betAdviceStat_ setText:@""];
	[actionAdviceStat_ setText:@""];
	[curCardCount_ setText:@"KO system"];
	
}

- (void)setLandscapeToView:(UIView *)view
{
	[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
	CGAffineTransform landscapeTransform = CGAffineTransformMakeRotation(M_PI_2);
	landscapeTransform = CGAffineTransformTranslate (landscapeTransform, +80.0, +100.0);
	[view setTransform:landscapeTransform];
	
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];	
}

- (void)dealloc {
    [super dealloc];
	[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO];
}

@end
